package com.woniuxy.caradmin.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.caradmin.entity.pojo.Feerecord;
import com.woniuxy.caradmin.entity.vo.FeerecordVo;
import com.woniuxy.caradmin.exception.CarException;
import com.woniuxy.caradmin.service.FeerecordService;
import com.woniuxy.caradmin.util.Constaint;
import com.woniuxy.caradmin.util.DataGridView;
import com.woniuxy.caradmin.util.R;
import com.woniuxy.caradmin.util.ResultCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author womenzu
 * @since 2020-12-07
 */
@RestController
@RequestMapping("/feerecord")
public class FeerecordController {
    @Autowired
    private FeerecordService feerecordService;

    @GetMapping("/feerecordList")
    @ApiOperation("查询所有的规费记录")
    public DataGridView selectAllFeeRecord(FeerecordVo feerecordVo) {
        Page<Object> page = PageHelper.startPage(feerecordVo.getPage(), feerecordVo.getLimit());
        List<Feerecord> feerecords = feerecordService.selectAllFeeRecord(feerecordVo);
        if (feerecords != null && feerecords.size() > 0) {
            return new DataGridView(page.getTotal(), feerecords);
        } else {
            throw new CarException(ResultCode.FEERECORDISNULL, Constaint.FEERECORDISNULL);
        }
    }

    //1 分页查询
    @GetMapping("/feerecords")
    @ApiOperation("分页查询所有的加油记录")
    public DataGridView selectAllBy(FeerecordVo feerecordVo) {
        Page<Object> page = PageHelper.startPage(feerecordVo.getPage(), feerecordVo.getLimit());
        List<Feerecord> feerecords = feerecordService.selectAllBy(feerecordVo);
        if (feerecords != null && feerecords.size() > 0) {
            return new DataGridView(page.getTotal(), feerecords);
        } else {
            throw new CarException(ResultCode.FEERECORDISNULL, Constaint.FEERECORDISNULL);
        }

    }

    //2 增加
    @GetMapping("/addFeeRecord")
    @ApiOperation("增加规费记录")
    public R addFeeRecord(Feerecord feerecord, HttpServletRequest httpServletRequest) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //得到
        String vid = httpServletRequest.getParameter("vid");
        String name = httpServletRequest.getParameter("name");
        String paytime = httpServletRequest.getParameter("paytime");
        String paynum = httpServletRequest.getParameter("paynum");
        String fuid = httpServletRequest.getParameter("fuid");
        String information = httpServletRequest.getParameter("information");
        String driverid = httpServletRequest.getParameter("driverid");
        String status = httpServletRequest.getParameter("status");//设置增加的状态
        //设置时间
        java.sql.Date t1 = null;
        try {
            Date st1 = sdf.parse(paytime);
            t1 = new java.sql.Date(st1.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //设置
        feerecord.setVid(Integer.parseInt(vid));
        feerecord.setName(name);
        feerecord.setPaytime(t1);
        feerecord.setPaynum(Double.valueOf(paynum));
        feerecord.setFuid(Integer.parseInt(fuid));
        feerecord.setInformation(information);
        feerecord.setDriverid(Integer.parseInt(driverid));
        feerecord.setStatus(Integer.parseInt(status));

        int addFeeRecord = feerecordService.addFeeRecord(feerecord);
        if (addFeeRecord != 0) {
            return R.ok().code(ResultCode.FEERECORDISSUC).message(Constaint.FEERECORDISSUC);
        } else {
            throw new CarException(ResultCode.FEERECORDISFAI, Constaint.FEERECORDISFAI);
        }
    }

    //3 修改
    @GetMapping("/updateFeeRecord")
    @ApiOperation("修改规费记录")
    public R updateFeeRecord(Feerecord feerecord) {
        int updateFeeRecord = feerecordService.updateFeeRecord(feerecord);
        if (updateFeeRecord != 0) {
            return R.ok().code(ResultCode.FEERECORDUPDATEISSUC).message(Constaint.FEERECORDUPDATEISSUC);
        } else {
            throw new CarException(ResultCode.FEERECORDUPDATEISFAI, Constaint.FEERECORDUPDATEISFAI);
        }
    }

    //4 删除
    @GetMapping("/deleteFeeRecord")
    @ApiOperation("删除规费记录")
    public R deleteFeeRecord(Integer[] ids) {
        boolean deleteFeeRecord = feerecordService.deleteFeeRecord(ids);
        if (deleteFeeRecord) {
            return R.ok().code(ResultCode.FEERECORDDDDELETEISSUC).message(Constaint.FEERECORDDDDELETEISSUC);
        } else {
            throw new CarException(ResultCode.FEERECORDDDELETEISFAI, Constaint.FEERECORDDDELETEISFAI);
        }
    }

    //5 配合修改
    @GetMapping("/feeRecordById")
    @ApiOperation("根据id查询规费记录")
    public R oilRecordById(Integer id) {
        Feerecord feeRecordById = feerecordService.feeRecordById(id);
        if (feeRecordById != null) {
            return R.ok().data("feeRecordById", feeRecordById);
        } else {
            throw new CarException(ResultCode.FEERECORDISNULL, Constaint.FEERECORDISNULL);
        }
    }


}

